<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { reactive, watch, inject, Ref, onBeforeUnmount } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { Row } from '@/api/logistics/vehicle'
import { AllUpload } from '@/components/AllUpload'
const { required } = useValidator()
const currentRow: Ref<Row> = inject('currentRow') as Ref<Row>
const { formRegister, formMethods } = useForm()

const { setValues, getFormData, getElFormExpose } = formMethods

const schema = reactive<FormSchema[]>([
  {
    field: '',
    label: '基础信息',
    component: 'Divider',
    colProps: { span: 28 },
    componentProps: {
      borderStyle: 'none',
      contentPosition: 'left'
    }
  },
  {
    field: 'no',
    label: '车牌号',
    component: 'Input',
    formItemProps: {
      rules: [required(), { message: '请输入内容', trigger: 'blur' }]
    },
    componentProps: {
      placeholder: '请输入内容'
    }
  },
  {
    field: 'type',
    label: '车型',
    component: 'Select',
    formItemProps: {
      rules: [required()]
    },
    componentProps: {
      placeholder: '请选择',
      options: [
        {
          label: '小面',
          value: 1
        },
        {
          label: '中面',
          value: 2
        },
        {
          label: '微面',
          value: 3
        },
        {
          label: '小货',
          value: 4
        },
        {
          label: '中货',
          value: 5
        },
        {
          label: '大货',
          value: 6
        },
        {
          label: '冷藏车',
          value: 7
        },
        {
          label: '私家车',
          value: 8
        }
      ]
    }
  },
  {
    field: 'length',
    label: '车长',
    component: 'Select',
    componentProps: {
      placeholder: '请选择',
      options: [
        {
          label: '4米2',
          value: 1
        },
        {
          label: '5米2',
          value: 2
        },
        {
          label: '6米8',
          value: 3
        },
        {
          label: '7米6',
          value: 4
        },
        {
          label: '9米6',
          value: 5
        },
        {
          label: '13米',
          value: 6
        },
        {
          label: '其他',
          value: 7
        }
      ]
    }
  },
  {
    field: 'remark',
    label: '备注信息',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'color',
    label: '车辆颜色',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'passenger_num',
    label: '核定载客',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'volume',
    label: '核载体积（m³）',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'load_weight',
    label: '核载重量（kg）',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'brand',
    label: '车辆品牌',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: '',
    label: '',
    component: 'Divider',
    colProps: { span: 28 }
  },
  {
    field: 'img',
    label: '车辆图片',
    formItemProps: {
      slots: {
        default: (data) => {
          return <AllUpload v-model={data.img} max={9} />
        }
      }
    }
  }
])

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    const formDataList = {
      id: formData.id || '',
      no: formData.no || '',
      type: formData.type || '',
      length: formData.length || '',
      remark: formData.remark || '',
      color: formData.color || '',
      passenger_num: formData.passenger_num || '',
      volume: formData.volume || '',
      load_weight: formData.load_weight || '',
      brand: formData.brand || '',
      img: formData.img ? formData.img.filter(Boolean).join(',') : ''
    }
    return formDataList
  }
}

const reset = async () => {
  const elForm = await getElFormExpose()
  elForm?.resetFields()
}

watch(
  () => currentRow.value,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

onBeforeUnmount(reset)

defineExpose({
  submit,
  reset
})
</script>

<template>
  <Form @register="formRegister" :schema="schema" />
</template>
